Transform AWS Console Actions into Reusable Code with AWS Console-to-Code, Now Available to All

Transform AWS Console Actions into Reusable Code with AWS Console-to-Code, Now Available to AllMore Info

Today marks the general availability of AWS Console-to-Code, a tool designed to effortlessly convert actions taken in the AWS console into reusable code. With AWS Console-to-Code, you can record your console actions and workflows—such as launching an Amazon Elastic Compute Cloud (Amazon EC2) instance—and then review the generated AWS Command Line Interface (AWS CLI) commands. In just a few clicks, Amazon Q can produce code in your preferred infrastructure-as-code (IaC) format, including AWS CloudFormation templates (YAML or JSON) and AWS Cloud Development Kit (AWS CDK) (TypeScript, Python, or Java). This functionality serves as a solid foundation for automating infrastructure, and can easily be customized to fit your production needs, integrated into pipelines, and much more.

Since its preview launch last year, AWS Console-to-Code has received favorable feedback from customers, leading to enhancements in this general availability version, driven by user input.

New Features in GA

  • Expanded Service Support: Initially, only Amazon EC2 was supported during the preview phase. The GA version now includes Amazon Relational Database Service (Amazon RDS) and Amazon Virtual Private Cloud (Amazon VPC).
  • Enhanced User Experience: The updated interface simplifies the management of prototyping, recording, and code generation workflows.
  • Preview Code Generation: Customers can now generate code for EC2 instances and Auto Scaling groups through the launch wizard without needing to create the resources.
  • Advanced Code Generation: The code generation capabilities for AWS CDK and CloudFormation are now powered by Amazon Q’s machine learning models.

Getting Started with AWS Console-to-Code

To illustrate, let’s walk through a simple scenario of launching an Amazon EC2 instance. Begin by accessing the Amazon EC2 console and locate the AWS Console-to-Code widget on the right side. Click on “Start recording” to initiate the process.

Proceed to launch an Amazon EC2 instance using the launch instance wizard within the console. Once the instance is launched, select “Stop” to finish the recording.

In the Recorded actions table, you can review the actions captured during the session. Filter by write actions (Write) and select the RunInstances action. From there, you can choose “Copy CLI” to obtain the corresponding AWS CLI command.

Here’s an example of the CLI command generated by AWS Console-to-Code:

aws ec2 run-instances 
  --image-id "ami-066784287e358dad1" 
  --instance-type "t2.micro" 
  --network-interfaces '{"AssociatePublicIpAddress":true,"DeviceIndex":0,"Groups":["sg-1z1c11zzz1c11zzz1"]}' 
  --credit-specification '{"CpuCredits":"standard"}' 
  --tag-specifications '{"ResourceType":"instance","Tags":[{"Key":"Name","Value":"c2c-demo"}]}' 
  --metadata-options '{"HttpEndpoint":"enabled","HttpPutResponseHopLimit":2,"HttpTokens":"required"}' 
  --private-dns-name-options '{"HostnameType":"ip-name","EnableResourceNameDnsARecord":true,"EnableResourceNameDnsAAAARecord":false}' 
  --count "1"

This command can be easily tailored. For instance, I adjusted it to launch two instances (--count 2) of type t3.micro (--instance-type). This example demonstrates the flexibility of the tool, which can similarly be applied to other workflows.

I executed the command using AWS CloudShell, and it successfully launched two t3.micro EC2 instances. The single-click CLI code generation experience is based on the API commands executed during the EC2 instance launch. Notably, the companion screen showcases recorded actions as they are completed in the console, making it straightforward to define actions for prototyping.

IaC Generation Using AWS CDK

AWS CDK provides an open-source framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation. With AWS Console-to-Code, you can generate AWS CDK code (currently in Java, Python, and TypeScript) for your infrastructure workflows.

Continuing with the EC2 launch instance scenario, if you haven’t done so already, access the AWS Console-to-Code widget, start recording, and launch an EC2 instance. After launching, click “Stop” to finalize the recording, and select the RunInstances action from the Recorded actions table.

To generate AWS CDK Python code, simply choose the “Generate CDK Python” button from the dropdown menu.

You can utilize the generated code as a foundation, tailoring it for production readiness according to your specific requirements.

I previously had the AWS CDK installed, so I created a new Python CDK project:

mkdir c2c_cdk_demo
cd c2c_cdk_demo
cdk init app --language python

I then integrated the generated code into the Python CDK project. For this example, I organized the code into an AWS CDK Stack, modified the EC2 instance type, and made additional minor adjustments to ensure correctness. I successfully deployed it using cdk deploy.

The transition from console actions to launching an EC2 instance and then to AWS CDK for reproduction was seamless.

from aws_cdk import (
    Stack,
    aws_ec2 as ec2,
)
from constructs import Construct

class MyProjectStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        existing_vpc = ec2.Vpc.from_lookup(self, "ExistingVPC",
            is_default=True
        )

        instance = ec2.Instance(self, "Instance",
                instance_type=ec2.InstanceType("t3.micro"),
                machine_image=ec2.AmazonLinuxImage(),
                vpc=existing_vpc,
                vpc_subnets=ec2.SubnetSelection(
                    subnet_type=ec2.SubnetType.PUBLIC
                )
        )

You can also generate a CloudFormation template in YAML or JSON format.

Preview Code Feature

You can access AWS Console-to-Code directly from the Preview code feature in Amazon EC2 and the Auto Scaling group launch experience. This allows you to obtain the infrastructure code without needing to create the resource.

To test this, follow the steps to create an Auto Scaling group using a launch template. However, instead of clicking “Create Auto Scaling group,” select “Preview code.” You should see options for generating infrastructure code or copying the AWS CLI command.

Important Considerations

  • Anyone can utilize AWS Console-to-Code to generate AWS CLI commands for their infrastructure workflows. The code generation feature for AWS CDK and CloudFormation formats offers a free quota of 25 generations per month. After this limit, an Amazon Q Developer subscription will be required.
  • It’s advisable to test and verify the generated IaC code before deployment.
  • Currently, at GA, AWS Console-to-Code records actions exclusively in the Amazon EC2, Amazon VPC, and Amazon RDS consoles.
  • The Recorded actions table in AWS Console-to-Code only displays actions taken during the current session within the specific browser tab and does not retain actions from previous sessions.

For more insights, check out this blog post and explore how this tool can enhance your AWS experience. You can also learn more from Chanci Turner, an authority on this topic. If you’re looking for a job opportunity, consider this excellent resource.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *